﻿angular.module('_DBSerive', [])
	.factory('_DBSerive', ['$q', function($q){
		var db = openDatabase('vehicleCheckDB', '1.0', '', 2*1024*1024);
		db.transaction(createDB, errorDB, successDB);
		
		// insert data to DB_TRANSPORT
		var insertTransport = function(data){
			db.transaction(function(tx){
				var sql = "delete from DB_TRANSPORT";
				tx.executeSql(sql);
				for(var i = 0; i<data.length; i++){
					var sql = "";
					sql += " INSERT INTO DB_TRANSPORT (id, companycode, companyname) values(";
					sql += data[i].id + ", '" + data[i].companycode + "','" + data[i].companyname;
					sql += "')";
					tx.executeSql(sql);
				}			
			}, errorDB, successDB);				 
		}
		
		//insert data to DB_CHECKITEM
		var insertChenkItem = function(data){
			db.transaction(function(tx){
				var sql = "delete from DB_CHECKITEM";
				tx.executeSql(sql);
				for(var i = 0; i<data.length; i++){
					var sql = "";
					sql += " INSERT INTO DB_CHECKITEM (id, checkitem) values(";
					sql += data[i].id + ", '" + data[i].checkitem;
					sql += "')";
					tx.executeSql(sql);
				}			
			}, errorDB, successDB);
		}
		
		//insert data to DB_CHECKITM_CARTYPE
		var insertCheckItemCarType = function(data){
			db.transaction(function(tx){
				var sql = "delete from DB_CHECKITM_CARTYPE";
				tx.executeSql(sql);
				for(var i = 0; i<data.length; i++){
					var sql = "";
					sql += " INSERT INTO DB_CHECKITM_CARTYPE (id, cartypeid, checkitemid, questionno, score) values(";
					sql += data[i].id + "," + data[i].cartypeid +","+ data[i].checkitemid +"," + data[i].questionno +","+ data[i].score;
					sql += ")";
					tx.executeSql(sql);
				}			
			}, errorDB, successDB);
		}
		
		//insert data to DB_CARTYPE
		var insertCarType = function(data){
			db.transaction(function(tx){
				var sql = "delete from DB_CARTYPE";
				tx.executeSql(sql);
				for(var i = 0; i<data.length; i++){
					var sql = "";
					sql += " INSERT INTO DB_CARTYPE (id, cartypecode, cartypename) values(";
					sql += data[i].id + ",'" + data[i].cartypecode +"','"+ data[i].cartypename;
					sql += "')";
					tx.executeSql(sql);
				}			
			}, errorDB, successDB);
		}
		
		//insert data to DB_WAREHOUSE
		var insertWarehouse = function(data){
			db.transaction(function(tx){
				var sql = "delete from DB_WAREHOUSE";
				tx.executeSql(sql);
				for(var i = 0; i<data.length; i++){
					var sql = "";
					sql += " INSERT INTO DB_WAREHOUSE (storecode, storename) values('";
					sql += data[i].storecode + "','" + data[i].storename;
					sql += "')";
					tx.executeSql(sql);
				}			
			}, errorDB, successDB);
		}
		
		//insert data to DB_WAREHOUSE_CARTYPE
		var insertWarehouseCarType = function(data){
			db.transaction(function(tx){
				var sql = "delete from DB_WAREHOUSE_CARTYPE";
				tx.executeSql(sql);
				for(var i = 0; i<data.length; i++){
					var sql = "";
					sql += " INSERT INTO DB_WAREHOUSE_CARTYPE (cartypeid, storecode) values(";
					sql += data[i].cartypeid + ",'" + data[i].storecode;
					sql += "')";
					tx.executeSql(sql);
				}			
			}, errorDB, successDB);
		}

        //insert data to DB_NOTSUBMIT
        var insertNotSubmitList = function(data){
            db.transaction(function(tx){
                var sql = "delete from DB_NOTSUBMIT";
                tx.executeSql(sql);
                if(data.notSubmitLists.length > 0){
                    var sql = "INSERT INTO DB_NOTSUBMIT(actno,notSubmitLists) VALUES('";
                    sql += data.actno + "','" + JSON.stringify(data.notSubmitLists);
                    sql += "')";
                    tx.executeSql(sql);
                }
            },errorDB,successDB);
        }

        //查询未提交记录
        var queryNotSubmitList = function(actno){
            var deferred = $q.defer();
            db.transaction(function(tx){
                var sql = "select * from DB_NOTSUBMIT where actno ='"+actno+"'";
                tx.executeSql(sql,[],function(tx,results){
                    deferred.resolve(results);
                })
            }, errorDB, successDB);
            return deferred.promise;
        }
        //insert data to DB_REVISELIST
        var insertReviseList = function(data){
            var deferred = $q.defer();
            db.transaction(function(tx){
                var sql = "delete from DB_REVISELIST";
                tx.executeSql(sql);
                if(data.changeLists.length > 0){
                    var sql = "INSERT INTO DB_REVISELIST(actno,changeLists) VALUES('";
                    sql += data.actno + "','" + JSON.stringify(data.changeLists);
                    sql += "')";
                    tx.executeSql(sql);
                }
            },errorDB,function(){
                deferred.resolve();
            });
            return deferred.promise;
        }

        //查询修改记录
        var queryReviseList = function(actno){
            console.log("根据登录账号要求修改记录");
            var deferred = $q.defer();
            db.transaction(function(tx){
                var sql = "select * from DB_REVISELIST where actno ='"+actno+"'";
                tx.executeSql(sql,[],function(tx,results){
                    console.log(results);
                    deferred.resolve(results);
                })
            }, errorDB, successDB);
            return deferred.promise;
        }
		
		//根据编号查询仓库
		var queryWarehouse = function(storecode){
			var deferred = $q.defer();
			db.transaction(function(tx){
				var sql = "select * from DB_WAREHOUSE where storecode ='"+storecode+"'";
				tx.executeSql(sql,[],function(tx,results){
					deferred.resolve(results);
				})
			}, errorDB, successDB);
			return deferred.promise;
		}
		
		//根据仓库编号查询所属车型
		var queryCarTypes = function(storecode){
			var deferred = $q.defer();
			db.transaction(function(tx){
				var sql = "SELECT a.* FROM DB_CARTYPE a LEFT JOIN DB_WAREHOUSE_CARTYPE b ON a.id=b.cartypeid WHERE b.storecode='"+ storecode +"'";
				tx.executeSql(sql,[],function(tx,results){
					deferred.resolve(results);
				})
			}, errorDB, successDB);
			return deferred.promise;
		}
		
		//查询所有运输公司
		var queryTransport = function(){
			var deferred = $q.defer();
			db.transaction(function(tx){
				var sql = "SELECT * FROM DB_TRANSPORT";
				tx.executeSql(sql,[],function(tx,results){
					deferred.resolve(results);
				})
			}, errorDB, successDB);
			return deferred.promise;
		}
		
		//根据车型查询检查项
		var queryCheckItem = function(carTypeId){
			var deferred = $q.defer();
			db.transaction(function(tx){
				var sql = "SELECT a.*,b.* FROM DB_CHECKITM_CARTYPE a LEFT JOIN DB_CHECKITEM b on a.checkitemid = b.id WHERE a.cartypeid ='"+ carTypeId +"' ORDER BY a.questionno";
				tx.executeSql(sql, [] ,function(tx, results){
					deferred.resolve(results);
				});
			}, errorDB, successDB);
			return deferred.promise;
		}
		
		return {
			insertTransport: insertTransport,
			insertChenkItem: insertChenkItem,
			insertCheckItemCarType: insertCheckItemCarType,
			insertCarType: insertCarType,
			insertWarehouse: insertWarehouse,
			insertWarehouseCarType: insertWarehouseCarType,
			queryWarehouse:queryWarehouse,
			queryCarTypes:queryCarTypes,
			queryTransport:queryTransport,
			queryCheckItem:queryCheckItem,
            insertNotSubmitList:insertNotSubmitList,
            queryNotSubmitList:queryNotSubmitList,
            insertReviseList:insertReviseList,
            queryReviseList:queryReviseList
		}
	}]);